package com.xingkeduo.pojo.utils;

import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/6/10 16:37
 **/
@Slf4j
public class Base64Utils {

    final static Base64.Decoder DECODER = Base64.getDecoder();
    final static Base64.Encoder ENCODER = Base64.getEncoder();


    /**
     * 加密
     *
     * @param text:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/6/10 16:49
     */
    public static String encodeParam(String text) {
        try {
            byte[] bytes = text.getBytes("UTF-8");
            String encodeStr = ENCODER.encodeToString(bytes);
            return encodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.info("Base64Utils 加密方法失败");
            return text;
        }catch (Exception e) {
	        e.printStackTrace();
	        log.info("Base64Utils 加密方法失败");
	        return text;
	    }
    }


    /**
     * 解密
     *
     * @param text:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/6/10 16:48
     */
    public static String decoderParam(String text) {
        try {
        	if(StringUtils.isEmpty(text)) {
        		return "";
        	}
            String decodeStr = new String(DECODER.decode(text), "UTF-8");
            return decodeStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.info("Base64Utils解密方法失败");
            return text;
        }catch (Exception e) {
            e.printStackTrace();
            log.info("Base64Utils 解密方法失败");
            return text;
        }
        
    }


    public static void main(String[] args) {
        String str ="S29qYW4=";
//        String encodeParam = encodeParam(str);
//        System.out.println(encodeParam);
        String decoderParam = decoderParam(str);
        System.out.println(decoderParam + " aa");
    }
}
